﻿#region Imports
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion

namespace FBShortcutKeeper
{
    public partial class frmShortcutKeeper : Form
    {
        string path = Application.StartupPath + @"\_Keeper.txt";
        Encoding encod = Encoding.ASCII;

        public frmShortcutKeeper()
        {
            InitializeComponent();
            //this.Height = 106;

            try
            {
                //rtbUrl.Text = /*rtbDemo.Text =*/ Clipboard.GetText(TextDataFormat.Html);
                rtbUrl.Rtf = Clipboard.GetData(DataFormats.Rtf).ToString();
            }
            catch (Exception)
            {
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnKeep_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(rtbUrl.Text))
            {
                if (rtbUrl.Text.IndexOf('<') > 0 && rtbUrl.Text.IndexOf('>') > 0)
                {
                    string url = rtbUrl.Text.Substring(rtbUrl.Text.IndexOf('<') + 1, rtbUrl.Text.IndexOf('>') - rtbUrl.Text.IndexOf('<') - 1);
                    rtbUrl.Text = rtbUrl.Text.Replace(url, url.Split('?').First());
                }
            }

            if (!File.Exists(path)) File.CreateText(path);
            File.AppendAllText(path, rtbUrl.Text + "\r\n", encod);

            rtbUrl.Focus();
            rtbUrl.SelectAll();

            btnLoad_Click(sender, e);
        }

        private void rtbDemo_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            //Process.Start("IExplore.exe", e.LinkText);
            brwsUrl.Url = new Uri(e.LinkText);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            // Clipboard.SetData(DataFormats.Rtf, File.ReadAllText(path, encod));
            // rtbDemo.Rtf = Clipboard.GetData(DataFormats.Rtf).ToString();
            rtbDemo.Text = File.ReadAllText(path, encod);
        }
    }
}


//DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Html);
//if(richTextBox1.CanPaste(myFormat))
//{
//    richTextBox1.Paste(myFormat);
//    return true;
//}